package com.jyw.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.domain.vo.SysTeamUserOneVO;
import com.jyw.system.domain.vo.SysTeamUserVO;
import com.jyw.system.domain.vo.TeamCacheVo;
import com.jyw.system.utils.CacheVersionUtils;
import com.jyw.system.utils.TeamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jyw.system.mapper.SysTeamMapper;
import com.jyw.system.domain.SysTeam;
import com.jyw.system.service.ISysTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 小组管理Service业务层处理
 *
 * @author system
 * @date 2023-08-07
 */
@Service
public class SysTeamServiceImpl extends ServiceImpl<SysTeamMapper, SysTeam> implements ISysTeamService {
    @Resource
    private SysTeamMapper sysTeamMapper;

    /**
     * 查询小组管理
     *
     * @param id 小组管理主键
     * @return 小组管理
     */
    @Override
    public SysTeam selectSysTeamById(Long id) {
        return sysTeamMapper.selectSysTeamById(id);
    }

    /**
     * 查询小组管理列表
     *
     * @param sysTeam 小组管理
     * @return 小组管理
     */
    @Override
    public List<SysTeam> selectSysTeamList(SysTeam sysTeam) {
        return sysTeamMapper.selectSysTeamList(sysTeam);
    }

    /**
     * 新增小组管理
     *
     * @param sysTeam 小组管理
     * @return 结果
     */
    @Override
    public int insertSysTeam(SysTeam sysTeam) {
        sysTeam.setCreateTime(DateUtils.getNowDate());
        sysTeam.setCreateBy(SecurityUtils.getUsername());
        int i = sysTeamMapper.insertSysTeam(sysTeam);
        CacheVersionUtils.teamVersionUp();
        return i;
    }

    /**
     * 修改小组管理
     *
     * @param sysTeam 小组管理
     * @return 结果
     */
    @Override
    public int updateSysTeam(SysTeam sysTeam) {
        sysTeam.setUpdateTime(DateUtils.getNowDate());
        sysTeam.setUpdateBy(SecurityUtils.getUsername());
        int i = sysTeamMapper.updateSysTeam(sysTeam);
        CacheVersionUtils.teamVersionUp();
        return i;
    }

    /**
     * 批量删除小组管理
     *
     * @param ids 需要删除的小组管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTeamByIds(Long[] ids) {
        return sysTeamMapper.deleteSysTeamByIds(ids);
    }

    /**
     * 删除小组管理信息
     *
     * @param id 小组管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTeamById(Long id) {
        return sysTeamMapper.deleteSysTeamById(id);
    }

    @Override
    public List<TeamCacheVo> getCache() {
        List<SysTeam> sysTeamList = TeamUtils.cacheList();
        return sysTeamList.stream().map(TeamCacheVo::new).collect(Collectors.toList());
    }

    @Override
    public List<SysTeamUserOneVO> getSysTeamUser() {
        List<SysTeamUserOneVO> oneVOList = this.baseMapper.getSysTeam();
        //添加未分组的
        SysTeamUserOneVO oneVO = new SysTeamUserOneVO();
        oneVO.setTeamId(null);
        oneVO.setTeamName("未分组");
        oneVOList.add(oneVO);

        for (SysTeamUserOneVO vo:oneVOList){
            if (vo.getTeamId()!=null){
                List<SysTeamUserVO> sysTeamUser = this.baseMapper.getSysTeamUser(vo.getTeamId());
                vo.setVoList(sysTeamUser);
            }else {
                List<SysTeamUserVO> vos = this.baseMapper.getSysTeamUngrouped();
                vo.setVoList(vos);
            }
        }
        return oneVOList;
    }
}
